package com.cy.pj.sys.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.Node;
import com.cy.pj.sys.Dao.SysMenuDao;
import com.cy.pj.sys.Dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
@Service
public class SysMenuServiceImpl implements SysMenuService {
	 @Autowired
     private SysMenuDao sysMenuDao;
	 @Autowired
	 private SysRoleMenuDao sysRoleMenuDao;
	@Override
	public List<Map<String, Object>> findObjects() {
		List<Map<String,Object>> list=
				sysMenuDao.findObjects();
			if(list==null||list.size()==0)
			throw new ServiceException("没有对应的菜单信息");
			return list;
	

}
	@Override
	public int deleteObject(Integer id) {
		//1.判定参数
		if(id==null||id<1) {
			throw new IllegalArgumentException("参数值无效");
		}
		//2.统计菜单子元素并校验
		int childCount=sysMenuDao.getChildCount(id);
		if(childCount>0) {
			throw new ServiceException("请先删除子菜单");
		}
		//3.先删除关系数据
		sysRoleMenuDao.deleteObjectByMenuId(id);
		//4.再删除自身信息
		int rows=sysMenuDao.deleteObject(id);
		if(rows==0)
		throw new ServiceException("此菜单可能已经不存在");
		
		return rows;
	}
	@Override
	public List<Node> findZtreeMenuNodes() {
		return sysMenuDao.findZtreeMenuNodes();
	}

	@Override
	public int saveObject(SysMenu entity) {
		//1.合法验证
		if(entity==null) {
			throw new ServiceException("保存内容不能为空");
		}
		if(StringUtils.isEmpty(entity)) {
			throw new ServiceException("菜单名不能为空");
		}
		//2.保存数据
		int  rows;
		try {
			rows = sysMenuDao.insertObject(entity);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("保存失败");
		}
		return rows;
	}

}